﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;

namespace Golf.WebUI
{
    public partial class Index : System.Web.UI.Page
    {
        /// <summary>
        /// 产品 bll是咧
        /// </summary>
        private Golf.BLL.Golf_Product bll = new Golf.BLL.Golf_Product();
        /// <summary>
        /// 获取的产品实体
        /// </summary>
        private Golf.Model.Golf_Product model = new Golf.Model.Golf_Product();
        /// <summary>
        /// 获取主机名称
        /// </summary>
        public string http_Host = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            http_Host = ThumNail.GetHttpHost();
            if (!Page.IsPostBack)
            {
                BindPro_1();
                BindPro_2();
                BindPro_3();
                Bind_ZheKou(); Bind_Customer_Rated_Pro();
                Bind_Best_Selling_Pro();
                Bind_Recently_Added();
            }
        }
        /// <summary>
        /// 绑定产品
        /// </summary>
        public void BindPro_1()
        {
            //我要做个处理
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(8, "1=1", "Pro_AddTime desc").Tables[0]);
            foreach (var item in list)
            {
                //只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            Repeater1.DataSource = list;
            Repeater1.DataBind();
            Script_Write(list);

        }
        /// <summary>
        /// 绑定产品
        /// </summary>
        public void BindPro_2()
        {
            //我要做个处理
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(8, "Pro_Save>10 and Pro_Save<=30", "Pro_Save desc").Tables[0]);
            foreach (var item in list)
            {
                //只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            rep_Two.DataSource = list;
            rep_Two.DataBind();
            Script_Write(list);

        }
        /// <summary>
        /// 绑定产品
        /// </summary>
        public void BindPro_3()
        {
            //我要做个处理
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(8, "Pro_Save>30 and Pro_Save<=40", "Pro_AddTime desc").Tables[0]);
            foreach (var item in list)
            {
                //只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            rep_Three.DataSource = list;
            rep_Three.DataBind();
            Script_Write(list);

        }
        #region   生成页面脚本    鼠标移动到上面的
        /// <summary>
        /// 生成页面脚本    鼠标移动到上面的
        /// </summary>
        public void Script_Write(List<Golf.Model.Golf_Product> list)
        {
            StringBuilder sbScript = new StringBuilder();
            sbScript.Append(" <script language=\"JavaScript\">  \n");
            foreach (var item in list)
            {
                //ID，名称，名优产品，HREF，图片，CommercialMsg，价格，规格，保存，评价，视频，缺货，折扣
                //ID, Name, BrandName, Href, Image, CommercialMsg, Price, RRP, Save, Rating, Video, OOS, Discount
                sbScript.Append("MAD.products.addProduct(new MAD.entities.product(\"" + item.Id + "\", \" " + item.Pro_Name + "\", \"" + GetBrandName((int)item.Pro_BrandTypeId) + "\", \"ProductDetail.aspx?id=" + item.Id + "\", \"ImageProducts/" + item.Pro_PicBig.Split(',')[0] + "\", \"" + item.Pro_Name + "\", \"" + item.Pro_PromotionPrice + "\", \"" + item.Pro_MarketPrice + "\", \"" + item.Pro_Save + "\", \"" + item.Pro_Star + "\", \"1\")); \n");
            }
            sbScript.Append(" </script> \n");
            script_pro.InnerHtml += sbScript.ToString();
        }
        #endregion
        /// <summary>
        /// 绑定 促销  商品
        /// </summary>
        public void Bind_ZheKou()
        {
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(8, 1, " IsPromotion='true'").Tables[0]);
            foreach (var item in list)
            {
                ////只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            Repeater2.DataSource = list;
            Repeater2.DataBind();
            Script_Write(list);
        }

        /// <summary>
        /// 绑定 最近添加  商品
        /// </summary>
        public void Bind_Recently_Added()
        {
            // order by  Pro_AddTime desc 按时间降序排列
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(18, "", " Pro_AddTime desc").Tables[0]);
            foreach (var item in list)
            {
                ////只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            Repeater4.DataSource = list;
            Repeater4.DataBind();
            // Script_Write(list);
        }
        /// <summary>
        /// 卖得最好的
        /// </summary>
        public void Bind_Best_Selling_Pro()
        {
            DataSet ds = Maticsoft.DBUtility.DbHelperSQL.Query("select b.ProductId,COUNT(*) aa from dbo.Golf_OrderProduct b where 1=1 group by b.ProductId order by aa desc");
            DataTable dt = ds.Tables[0];
            StringBuilder sb = new StringBuilder();
            int rowsCount = dt.Rows.Count;
            if (rowsCount > 0)
            {
                for (int n = 0; n < rowsCount; n++)
                {
                    if (dt.Rows[n]["ProductId"] != null && dt.Rows[n]["ProductId"].ToString() != "")
                    {
                        sb.Append(dt.Rows[n]["ProductId"].ToString() + ",");
                    }
                }
            }
            string sb1 = "";
            if (sb.Length != 0)
            {
                sb1 = sb.ToString().Substring(0, sb.Length - 1);
            }
            if (sb1.Length == 0)
            {
                return;
            }
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(6, 1, " id in(" + sb1 + ")").Tables[0]);
            foreach (var item in list)
            {
                ////只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            Repeater3.DataSource = list;
            Repeater3.DataBind(); 
            Script_Write(list);
        }


        /// <summary>
        /// 客户评论最好的
        /// </summary>
        public void Bind_Customer_Rated_Pro()
        {
            // 
            List<Golf.Model.Golf_Product> list = bll.DataTableToList(bll.GetList(6, "", "  Pro_Star desc").Tables[0]);
            foreach (var item in list)
            {
                ////只加载第一张图片  有点bug呀  呵呵
                string[] temp = item.Pro_Picture.Split(',');
                item.Pro_Picture = temp[0];
                //处理 节省值
                item.Pro_Save = Golf.GetSave._GetSave(item.Pro_MarketPrice, (decimal)item.Pro_PromotionPrice);
            }
            rep_Cus_best.DataSource = list;
            rep_Cus_best.DataBind();
            Script_Write(list);
        }
        /// <summary>
        /// 获取 品牌  多个地方调用
        /// </summary>
        /// <returns></returns>
        public string GetBrandName(int Model_id)
        {
            Golf.BLL.Golf_BrandType brand = new Golf.BLL.Golf_BrandType();
            Golf.Model.Golf_BrandType model = brand.GetModel(Model_id);
            if (model == null) return "";
            else return model.TypeName;
        }
    }
}
